<template>
  <div id="clues">
    <header class="clearfix">
      <div class="title" :style="{ 'margin-left': isMobile ? '8px' : '30px' }">
        设计一次通过率
      </div>
    </header>
     <div class="sub-header">
        <div class="select-option">
          <div class="item">
            <div class="select" @click="changeOption(0)" :class="{'selected': !selected}"></div>
            <div class="text">当月</div>
          </div>
          <div class="item">
            <div class="select" @click="changeOption(1)" :class="{'selected': selected}"></div>
            <div class="text">累计</div>
          </div>
        </div>
        <div class="status">
          <div class="item">
            <div class="color-close close"></div>
            <div class="text">一次通过的<br />图纸</div>
          </div>
          <div class="item">
            <div class="color-unclose close"></div>
            <div class="text">一次未通过<br />的图纸</div>
          </div>
        </div>
      </div>
    <div class="section" v-if="cluesList.length">
      <ul
        :style="{
          width:
            cluesList.length * 180 < ulWidth
              ? ulWidth + 'px'
              : cluesList.length * 180 + 'px',
        }"
      >
        <li
          class="chart-item"
          v-for="(item, index) in cluesList"
          :key="index"
          :style="{
            width:
              cluesList.length * 180 < ulWidth
                ? ulWidth / cluesList.length + 'px'
                : '180px',
          }"
        >
          <Charts
            :ref="`ids${index}`"
            :isMobile="isMobile"
            :data="item.list"
            :pieId="`ids${index}`"
            :top="top"
            :title="item.title"
          ></Charts>
        </li>
      </ul>
    </div>
  </div>
</template>
<script>

import { QHSEManagementDesign } from '../../../api/api'
import Charts from './pie_chart.vue'
export default {
  data () {
    return {
      selected: 0,
      top: true,
      ulWidth: '', // 整体容器宽度
      isMobile: false,
      cluesList: [], // chartData // 线索数据
      clueData: []
    }
  },
  components: { Charts },
  prop: {
    isMobile: {
      type: Boolean,
      default: () => {
        return false
      }
    }
  },
  methods: {
    changeOption: function (select) {
      this.selected = select
      this.getData(JSON.parse(JSON.stringify(this.clueData)), select)
      this.cluesList.forEach((element, i) => {
        this.$nextTick(() => {
          this.$refs['ids' + i] && this.$refs['ids' + i][0] && this.$refs['ids' + i][0].readerChart()
        })
      })
    },

    getData: function (response, select) {
      let listArr = []
      response.forEach((item) => {
        const obj = {
          title: item.issuedBy,
          list: [{
            'name': '一次性通过',
            percent: select ? item.onePassRate : item.monthOnePassRate,
            'value': select ? item.onePassDrawing : item.monthOnePassDrawing
          }, {
            percent: select ? item.onePassRate : item.monthOnePassRate,
            'name': '一次性未通过',
            'value': select ? item.oneFailPassDrawing : item.monthOneFailPassDrawing
          } ]
        }
        listArr.push(obj)
      })
      this.cluesList = listArr
      this.cluesList.forEach((element, i) => {
        this.$nextTick(() => {
          this.$refs['ids' + i] && this.$refs['ids' + i][0] && this.$refs['ids' + i][0].readerChart()
        })
      })
    },

    getClueData: async function () {
      const response = await QHSEManagementDesign(this.$route.query.projectId) || []
      this.clueData = response || []
      this.getData(JSON.parse(JSON.stringify(this.clueData)), 0)
      // await getIndexAllAreaClue({}).then((response) => {
      //   this.cluesList = response
      // })
    }
  },
  mounted: async function () {
    await this.getClueData()
    this.ulWidth = $('#clues').width()
    this.$EventBus.$on('loadEnd', () => {
      this.ulWidth = $('#clues').width()
      this.changeOption(this.selected)
    })
  }
}
</script>
<style lang="scss" scoped>
#clues {
  height: 100%;
  border: 1px solid #ffffff;
  position: relative;
  header {
    display: flex;
    height: 40px;
    width: 100%;
    margin-bottom: 2px;
    margin-top: 16px;
    .title {
      margin-left: 30px;
      margin-top: 11px;
      height: 18px;
      line-height: 18px;
      padding-left: 10px;
      border-left: 2px solid #008bf3;
      font-weight: 700;
      font-size: 17px;
      color: #ffffff;
    }
  }
  .section {
    overflow-x: scroll;
    overflow-y: hidden;
    // background: #4a4e6080;
    position: absolute;
    bottom: 40px;
    width:100%;
  }
  .sub-header {
    height: 100px;
    position: relative;
    .select-option {
      z-index: 1000;
      position: absolute;
      display: flex;
      width: 140px;
      left: 30px;
      height: 20px;
      top: 10px;
      .item {
        flex: 1;
        display: flex;
        margin-right: 20px;
        align-items: center;
        .text {
          color: #fff;
          font-size: 14px;
          font-weight: 600;
          text-align: center;
        }
        .select {
          cursor: pointer;
          margin-right: 6px;
          width: 10px;
          height: 10px;
          background: #fff;
          position: relative;
          i {
            color: #00b4f6;
            font-size: 16px;
            font-weight: 700;
            position: absolute;
            left: -1px;
          }
        }
        .selected {
          background: url('./ok.png') center center no-repeat;
          background-size: 10px 10px;
        }
      }
    }
    .status {
      position: absolute;
      display: flex;
      width: 280px;
      right: 0;
      top: 10px;
      .item {
        flex: 1;
        display: flex;
        margin-right: 20px;
        .text {
          color: #fff;
          font-size: 14px;
          font-weight: 700;
          text-align: center;
        }
        .close {
          margin-right: 6px;
          width: 20px;
          height: 20px;
        }
        .color-close {
          background: #9ed286;
        }
        .color-unclose {
          background: #00b3f6;
        }
      }
    }
  }
  ul {
    height: 204px;
    // padding-top: 100px !important;
    .chart-item {
      display: table-cell;
      float: left;
      position: relative;
      height: 100%;
    }
  }
}
.loading-header {
  border-bottom: 2px solid #eff0f4;
  height: 40px;
  .title-loading {
    height: 20px;
    margin-top: 10px;
    width: 60%;
  }
}
</style>
